package com.liuzhilong.infrastructure.framework.spring.aop;

import com.liuzhilong.infrastructure.framework.spring.component.TestAopTarget;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 * springaop的测试类
 * @author longandai@163.com
 */
@Aspect
@Component
public class TestAop {
    @PostConstruct
    public void init() {
        System.out.println(TestAop.class.getName()+" 我被初始化了呢");
    }
    //代理特定类的所有方法
    @Pointcut("execution(* com.liuzhilong.infrastructure.framework.spring.component.MyComponent.*(..))")
    public void testPointCutPackage(){

    }

    /**
     * 代理所有被 TestAnno 注解的类型
     */
    @Pointcut("@annotation(TestAnno)")
    public void testPointCutAnno(){

    }
    @Before(value="testPointCutPackage(),testPointCutAnno()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("com.liuzhilong.infrastructure.framework.spring.aop.TestAop.doBefore");
    }

    @Around(value="testPointCutAnno()")
    public Object  doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("com.liuzhilong.infrastructure.framework.spring.aop.TestAop.doAround before");
        Object result = null;
        if(joinPoint.getTarget() instanceof TestAopTarget && joinPoint.getSignature().getName().equalsIgnoreCase("doSome") ){
            System.out.println("拦截到了方法 com.liuzhilong.infrastructure.framework.spring.component.TestAopTarget.doSome");
            result = joinPoint.proceed(new String[]{"proxy name"});
        }else {
            result = joinPoint.proceed();

        }
        System.out.println("com.liuzhilong.infrastructure.framework.spring.aop.TestAop.doAround after");

        return result;
    }

}
